package com.th.supcom.pds.impl.service.internal.config;

import java.util.List;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.PhamWindowConfig;
import com.th.supcom.pds.impl.common.PdsConstants;
import com.th.supcom.pds.impl.manager.PhamWindowConfigManager;
import com.th.supcom.pds.internal.api.config.PhamWindowConfigService;

public class PhamWindowConfigServiceImpl implements PhamWindowConfigService
{
    private PhamWindowConfigManager m_phamWindowConfigManager;
    private TranslatorManager m_translatorManager;

    public void setPhamWindowConfigManager (PhamWindowConfigManager phamWindowConfigManager)
    {
        m_phamWindowConfigManager = phamWindowConfigManager;
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    @Override
    public void addPhamWindowConfig (PhamWindowConfig phamWindowConfig)
    {
        s_logger.info ("调用addPhamWindowConfig参数：phamWindowConfig:{}", new Object[]
        { phamWindowConfig });
        if (null == phamWindowConfig)
        {
            throw new IllegalArgumentException ("保存药房窗口属性列表时，对象不能为空");
        }
        this.checkWindowConfig (phamWindowConfig);
        this.checkPhamWindowConfigExistence (phamWindowConfig, true);
        this.checkPhamWindowIPExistence (phamWindowConfig, true);
        try
        {
            m_phamWindowConfigManager.addPhamWindowConfig (phamWindowConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("保存药房窗口属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("保存药房窗口属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    /**
     * 校验窗口属性配置
     * 
     * @param phamWindowConfig
     */
    private void checkWindowConfig (PhamWindowConfig phamWindowConfig)
    {
        if (StringUtils.isEmpty (phamWindowConfig.getWindowIp ()))
        {
            throw new IllegalArgumentException ("保存药房窗口属性列表时，IP地址不能为空");
        }
        if (StringUtils.isEmpty (phamWindowConfig.getWindowName ()))
        {
            throw new IllegalArgumentException ("保存药房窗口属性列表时，窗口名称不能为空");
        }
        if (StringUtils.isEmpty (phamWindowConfig.getDeptCode ()))
        {
            throw new IllegalArgumentException ("保存药房窗口属性列表时，药房编码不能为空");
        }
        if (StringUtils.isEmpty (phamWindowConfig.getServiceType ()))
        {
            throw new IllegalArgumentException ("保存药房窗口属性列表时，服务类型不能为空");
        }
        if (null == phamWindowConfig.getOpenFlag ())
        {
            throw new IllegalArgumentException ("保存药房窗口属性列表时，开放标识不能为空");
        }
    }

    /**
     * 校验数据库中是否已存在当前药房窗口配置
     * 
     * @param phamWindowConfig
     * @param isAdd 添加时，true；更新时，false
     */
    private void checkPhamWindowConfigExistence (PhamWindowConfig phamWindowConfig, boolean isAdd)
    {
        PhamWindowConfig condition = new PhamWindowConfig ();
        condition.setDeptCode (phamWindowConfig.getDeptCode ());
        condition.setWindowName (phamWindowConfig.getWindowName ());
        int count = this.findPhamWindowConfigCountByExample (condition);
        if (isAdd)
        {
            if (count != 0)
            {
                throw new BusinessException ("该药房窗口属性配置已存在");
            }
            return;
        }
        if (count > 1)
        {
            throw new BusinessException ("该药房机器属性配置已存在");
        }
    }

    /**
     * 校验当前窗口分配IP地址已被占用
     * 
     * @param phamWindowConfig
     * @param isAdd 添加时，true；更新时，false
     */
    private void checkPhamWindowIPExistence (PhamWindowConfig phamWindowConfig, boolean isAdd)
    {
        PhamWindowConfig condition = new PhamWindowConfig ();
        condition.setWindowIp (phamWindowConfig.getWindowIp ());
        int count = this.findPhamWindowConfigCountByExample (condition);
        if (isAdd)
        {
            if (count != 0)
            {
                throw new BusinessException ("该药房窗口分配ip已被占用！");
            }
            return;
        }
        if (count > 1)
        {
            throw new BusinessException ("该药房窗口分配ip已被占用！");
        }
    }

    @Override
    public void modifyPhamWindowConfig (PhamWindowConfig phamWindowConfig)
    {
        s_logger.info ("调用modifyPhamWindowConfig参数：phamWindowConfig:{}", new Object[]
        { phamWindowConfig });
        if (null == phamWindowConfig || null == phamWindowConfig.getPhamWindowConfigId ())
        {
            throw new IllegalArgumentException ("删除药房窗口属性列表时，对象不能为空");
        }
        this.checkWindowConfig (phamWindowConfig);
        this.checkPhamWindowConfigExistence (phamWindowConfig, false);
        this.checkPhamWindowIPExistence (phamWindowConfig, false);
        try
        {
            m_phamWindowConfigManager.modifyPhamWindowConfig (phamWindowConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药房窗口属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药房窗口属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public void modifyPhamWindowConfigByExample (PhamWindowConfig phamWindowConfig)
    {
        s_logger.info ("调用modifyPhamWindowConfigByExample参数：phamWindowConfig:{}", new Object[]
        { phamWindowConfig });
        if (null == phamWindowConfig || null == phamWindowConfig.getPhamWindowConfigId ())
        {
            throw new IllegalArgumentException ("删除药房窗口属性列表时，对象不能为空");
        }
        this.checkPhamWindowConfigExistence (phamWindowConfig, false);
        this.checkPhamWindowIPExistence (phamWindowConfig, false);
        try
        {
            m_phamWindowConfigManager.modifyPhamWindowConfigByExample (phamWindowConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药房窗口属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药房窗口属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public void removePhamWindowConfig (PhamWindowConfig phamWindowConfig)
    {
        s_logger.info ("调用removePhamWindowConfig参数：phamWindowConfig:{}", new Object[]
        { phamWindowConfig });
        if (null == phamWindowConfig || null == phamWindowConfig.getPhamWindowConfigId ())
        {
            throw new IllegalArgumentException ("删除药房窗口属性列表时，主键不能为空");
        }
        try
        {
            m_phamWindowConfigManager.removePhamWindowConfigByID (phamWindowConfig.getPhamWindowConfigId ());
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键删除药房窗口属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException (
                                         "通过主键删除药房窗口属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (),
                                         e);
        }
    }

    @Override
    public PhamWindowConfig getPhamWindowConfigById (Long phamWindowConfigId)
    {
        s_logger.info ("调用getPhamWindowConfigById参数：phamWindowConfigId:{}", new Object[]
        { phamWindowConfigId });
        if (null == phamWindowConfigId)
        {
            throw new IllegalArgumentException ("通过主键查询药房窗口属性列表时，主键不能为空");
        }
        try
        {
            PhamWindowConfig phamWindowConfig = m_phamWindowConfigManager.getPhamWindowConfigByID (phamWindowConfigId);
            phamWindowConfig = m_translatorManager.translate (phamWindowConfig);
            return phamWindowConfig;
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键查询药房窗口属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException (
                                         "通过主键查询药房窗口属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (),
                                         e);
        }
    }

    @Override
    public List <PhamWindowConfig> findPhamWindowConfigByExample (PhamWindowConfig phamWindowConfig)
    {
        s_logger.info ("调用findPhamWindowConfigByExample参数：phamWindowConfig:{}", new Object[]
        { phamWindowConfig });
        if (null == phamWindowConfig)
        {
            throw new IllegalArgumentException ("查询药房窗口属性列表时，查询条件不能为空");
        }
        try
        {
            List <PhamWindowConfig> list = m_phamWindowConfigManager.findPhamWindowConfigListByExample (phamWindowConfig);
            list = m_translatorManager.translate (list);
            return list;
        }
        catch (Exception e)
        {
            s_logger.error ("查询药房窗口属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询药房窗口属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public int findPhamWindowConfigCountByExample (PhamWindowConfig phamWindowConfig)
    {
        s_logger.info ("调用findPhamWindowConfigCountByExample参数：phamWindowConfig:{}", new Object[]
        { phamWindowConfig });
        if (null == phamWindowConfig)
        {
            throw new IllegalArgumentException ("查询药房窗口属性列表时，查询条件不能为空");
        }
        try
        {
            int count = m_phamWindowConfigManager.findPhamWindowConfigCountByExample (phamWindowConfig);
            return count;
        }
        catch (Exception e)
        {
            s_logger.error ("查询药房窗口属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询药房窗口属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamWindowConfigServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
